/*******************************************************************************
 * FilePath    : /Software/Examples_Ble_MS1658/ble_app_peripheral_LED/Src/BSP/PWM.c
 * Version     : 1.0
 * Brief       : 
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-04-03 15:06:14
 * LastEditors : AndrewHu
 * LastEditTime: 2021-09-23 16:43:47
 * Description : 
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef sTim1Handle = {0};
TIM_HandleTypeDef sTim2Handle = {0};

TIM_OC_InitTypeDef sTim1OcInitHandle = {0};
TIM_OC_InitTypeDef sTim2OcInitHandle = {0};

TIM_BreakDeadTimeConfigTypeDef sBreakConfig = {0};
/* Private function ----------------------------------------------------------*/
/*******************************************************************************
 * Function    : Pwm_Init
 * Brief       : 
 * param        {*}
 * return       {*}
 * Description : 
 * Date        : 2021-09-23 15:28:06
 ******************************************************************************/
void Pwm_Init(void)
{
    __HAL_RCC_TIM1_CLK_ENABLE();
    __HAL_RCC_TIM2_CLK_ENABLE();

    /* Compute the prescaler value to have TIMx counter clock equal to TIMx_FREQ(10000) Hz */
    u32 ulPrescalerValue = (uint32_t)(HAL_RCC_GetPCLKFreq() / PWM_FREQ) - 1;

#if 1
    /* Set TIMx instance */
    sTim1Handle.Instance = TIM1;

    /* Initialize TIMx peripheral as follows:
       + Period = PWM_PERIOD(10000-1)
       + Prescaler = (APBClock/TIMx_FREQ) - 1
       + ClockDivision = 0
       + Counter direction = Up
  */
    sTim1Handle.Init.Period = PWM_PERIOD;
    sTim1Handle.Init.Prescaler = ulPrescalerValue;
    sTim1Handle.Init.ClockDivision = 0;
    sTim1Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    sTim1Handle.Init.RepetitionCounter = 0;
    sTim1Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

    if (HAL_TIM_PWM_Init(&sTim1Handle) != HAL_OK)
    {
        /* Initialization Error */
        Error_Handler();
    }

    sTim1OcInitHandle.OCMode = TIM_OCMODE_PWM1;
    sTim1OcInitHandle.OCPolarity = TIM_OCPOLARITY_HIGH;
    sTim1OcInitHandle.OCNPolarity = TIM_OCPOLARITY_HIGH;
    sTim1OcInitHandle.OCFastMode = TIM_OCFAST_ENABLE;
    sTim1OcInitHandle.OCIdleState = TIM_OCIDLESTATE_RESET;
    sTim1OcInitHandle.OCNIdleState = TIM_OCNIDLESTATE_RESET;

#endif
#if 1
    /* Set TIMx instance */
    sTim2Handle.Instance = TIM2;

    /* Initialize TIMx peripheral as follows:
       + Period = PWM_PERIOD(10000-1)
       + Prescaler = (APBClock/TIMx_FREQ) - 1
       + ClockDivision = 0
       + Counter direction = Up
  */
    sTim2Handle.Init.Period = PWM_PERIOD;
    sTim2Handle.Init.Prescaler = ulPrescalerValue;
    sTim2Handle.Init.ClockDivision = 0;
    sTim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    sTim2Handle.Init.RepetitionCounter = 0;
    sTim2Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

    if (HAL_TIM_PWM_Init(&sTim2Handle) != HAL_OK)
    {
        /* Initialization Error */
        Error_Handler();
    }

    sTim2OcInitHandle.OCMode = TIM_OCMODE_PWM1;
    sTim2OcInitHandle.OCPolarity = TIM_OCPOLARITY_HIGH;
    sTim2OcInitHandle.OCNPolarity = TIM_OCNPOLARITY_LOW;
    sTim2OcInitHandle.OCFastMode = TIM_OCFAST_ENABLE;
    sTim2OcInitHandle.OCIdleState = TIM_OCIDLESTATE_RESET;
    sTim2OcInitHandle.OCNIdleState = TIM_OCNIDLESTATE_RESET;

#endif

    /* Set the Break feature & Dead time */
    sBreakConfig.BreakState = TIM_BREAK_DISABLE;
    sBreakConfig.DeadTime = 3;
    sBreakConfig.OffStateRunMode = TIM_OSSR_ENABLE;
    sBreakConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
    sBreakConfig.LockLevel = TIM_LOCKLEVEL_1;
    sBreakConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW;
    sBreakConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;

    if (HAL_TIM_ConfigBreakDeadTime(&sTim1Handle, &sBreakConfig) != HAL_OK)
    {
        /* Configuration Error */
        Error_Handler();
    }

    // if (HAL_TIM_ConfigBreakDeadTime(&sTim2Handle, &sBreakConfig) != HAL_OK)
    // {
    //     /* Configuration Error */
    //     Error_Handler();
    // }
}
/*******************************************************************************
 * Function    : PwmSet_Ch1_R
 * Brief       : 红
 * param        {u16} duty
 * return       {*}
 * Description : 
 * Date        : 2021-09-23 15:42:52
 ******************************************************************************/
void PwmSet_Ch1_R(u16 duty)
{
    // if (duty)
    // {
    // sTim1OcInitHandle.Pulse = 1000 - duty;
    sTim1OcInitHandle.Pulse = duty;
    HAL_TIM_PWM_ConfigChannel(&sTim1Handle, &sTim1OcInitHandle, TIM_CHANNEL_2);
    HAL_TIM_PWMN_Start(&sTim1Handle, TIM_CHANNEL_2);
    // }
    // else
    // {
    //     HAL_TIM_PWM_Stop(&sTim1Handle, TIM_CHANNEL_2);
    //     HAL_GPIO_WritePin(LED_R_GPIO, LED_R_PIN, GPIO_PIN_RESET);
    // }
}
/*******************************************************************************
 * Function    : PwmSet_Ch2_G
 * Brief       : 绿
 * param        {u16} duty
 * return       {*}
 * Description : 
 * Date        : 2021-09-23 15:42:58
 ******************************************************************************/
void PwmSet_Ch2_G(u16 duty)
{
    // if (duty)
    // {
    // sTim1OcInitHandle.Pulse = 1000 - duty;
    sTim2OcInitHandle.Pulse = duty;
    HAL_TIM_PWM_ConfigChannel(&sTim2Handle, &sTim2OcInitHandle, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&sTim2Handle, TIM_CHANNEL_3);
    // }
    // else
    // {
    //     HAL_TIM_PWM_Stop(&sTim2Handle, TIM_CHANNEL_3);
    //     HAL_GPIO_WritePin(LED_G_GPIO, LED_G_PIN, GPIO_PIN_RESET);
    // }
}
/*******************************************************************************
 * Function    : PwmSet_Ch3_B
 * Brief       : 蓝
 * param        {u16} duty
 * return       {*}
 * Description : 
 * Date        : 2021-09-23 15:43:04
 ******************************************************************************/
void PwmSet_Ch3_B(u16 duty)
{
    // if (duty)
    // {
    // sTim1OcInitHandle.Pulse = 1000 - duty;
    sTim2OcInitHandle.Pulse = duty;
    HAL_TIM_PWM_ConfigChannel(&sTim2Handle, &sTim2OcInitHandle, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&sTim2Handle, TIM_CHANNEL_1);
    // }
    // else
    // {
    //     HAL_TIM_PWM_Stop(&sTim2Handle, TIM_CHANNEL_1);
    //     HAL_GPIO_WritePin(LED_B_GPIO, LED_B_PIN, GPIO_PIN_RESET);
    // }
}
/*******************************************************************************
 * Function    : PwmSet_Ch4_C
 * Brief       : 冷
 * param        {u16} duty
 * return       {*}
 * Description : 
 * Date        : 2021-09-23 15:42:46
 ******************************************************************************/
void PwmSet_Ch4_C(u16 duty)
{
    // if (duty)
    // {
    // sTim1OcInitHandle.Pulse = 1000 - duty;
    sTim2OcInitHandle.Pulse = duty;
    HAL_TIM_PWM_ConfigChannel(&sTim2Handle, &sTim2OcInitHandle, TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&sTim2Handle, TIM_CHANNEL_4);
    // }
    // else
    // {
    //     HAL_TIM_PWM_Stop(&sTim2Handle, TIM_CHANNEL_4);
    //     HAL_GPIO_WritePin(LED_C_GPIO, LED_C_PIN, GPIO_PIN_RESET);
    // }
}
/*******************************************************************************
 * Function    : PwmSet_Ch5_W
 * Brief       : 暖
 * param        {u16} duty
 * return       {*}
 * Description : 
 * Date        : 2021-09-23 15:42:37
 ******************************************************************************/
void PwmSet_Ch5_W(u16 duty)
{
    // if (duty)
    // {
    // sTim1OcInitHandle.Pulse = 1000 - duty;
    sTim2OcInitHandle.Pulse = duty;
    HAL_TIM_PWM_ConfigChannel(&sTim2Handle, &sTim2OcInitHandle, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&sTim2Handle, TIM_CHANNEL_2);
    // }
    // else
    // {
    //     HAL_TIM_PWM_Stop(&sTim2Handle, TIM_CHANNEL_2);
    //     HAL_GPIO_WritePin(LED_W_GPIO, LED_W_PIN, GPIO_PIN_RESET);
    // }
}
/* extern --------------------------------------------------------------------*/
